

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Models &mdash; Fityk 1.1.1 manual</title>
    <link rel="stylesheet" href="_static/fityk.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '1.1.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="shortcut icon" href="_static/fityk.ico"/>
    <link rel="top" title="Fityk 1.1.1 manual" href="index.html" />
    <link rel="next" title="Curve Fitting" href="fit.html" />
    <link rel="prev" title="Data" href="data.html" />
 


  </head>
  <body>
<div style="background-color: white; text-align: center; padding: 10px 10px 10px 10px">
 <a href="http://fityk.nieto.pl/"><img src="_static/fityk-banner.png" alt="" /></a>
</div>

      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<h3><a href="fityk-manual.html">Table Of Contents</a></h3>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="intro.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="getstarted.html">Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="lang.html">Mini-Language</a></li>
<li class="toctree-l1"><a class="reference internal" href="data.html">Data</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="">Models</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#variables">Variables</a></li>
<li class="toctree-l2"><a class="reference internal" href="#function-types-and-functions">Function Types and Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="#built-in-functions">Built-In Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="#variadic-functions">Variadic Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="#user-defined-functions-udf">User-Defined Functions (UDF)</a></li>
<li class="toctree-l2"><a class="reference internal" href="#cutoff">Cutoff</a></li>
<li class="toctree-l2"><a class="reference internal" href="#model-f-and-z">Model, F and Z</a></li>
<li class="toctree-l2"><a class="reference internal" href="#guessing-initial-parameters">Guessing Initial Parameters</a></li>
<li class="toctree-l2"><a class="reference internal" href="#displaying-information">Displaying Information</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="fit.html">Curve Fitting</a></li>
<li class="toctree-l1"><a class="reference internal" href="ref.html">All the Rest</a></li>
</ul>

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/model.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="models">
<span id="model"></span><h1>Models</h1>
<p>From <a class="reference external" href="http://www.nrbook.com/a/bookcpdf.php">Numerical Recipes</a>,
chapter 15.0:</p>
<blockquote>
<div>Given a set of observations, one often wants to condense and summarize
the data by fitting it to a &#8220;model&#8221; that depends on adjustable
parameters. Sometimes the model is simply a convenient class of
functions, such as polynomials or Gaussians, and the fit supplies the
appropriate coefficients. Other times, the model&#8217;s parameters come
from some underlying theory that the data are supposed to satisfy;
examples are coefficients of rate equations in a complex network of
chemical reactions, or orbital elements of a binary star. Modeling can
also be used as a kind of constrained interpolation, where you want to
extend a few data points into a continuous function, but with some
underlying idea of what that function should look like.</div></blockquote>
<p>This chapter shows how to construct the model.</p>
<p>Complex models are often a sum of many functions. That is why in Fityk
the model <em>F</em> is constructed as a list of component functions
and is computed as <img class="math" src="_images/math/75870e6ce52429cdac61f596a15826f179ae817e.png" alt="F = \sum_i f_i" style="vertical-align: -5px"/>.</p>
<p>Each component function <img class="math" src="_images/math/61c594c97b61f72af93af4e61894b3f16a3bbb30.png" alt="f_i" style="vertical-align: -4px"/> is one of predefined functions,
such as Gaussian or polynomial.
This is not a limitation, because the user can add any function
to the predefined functions.</p>
<p>To avoid confusion, the name <em>function</em> will be used only when referring
to a component function, not when when referring to the sum (model),
which mathematically is also a function. The predefined functions
will be sometimes called <em>function types</em>.</p>
<p>Function <img class="math" src="_images/math/566b01428cbc7f85e5dcac822761913d6fd48795.png" alt="f_i=f_i(x; \boldsymbol{a})" style="vertical-align: -4px"/> is a function of <em>x</em>,
and depends on a vector of parameters <img class="math" src="_images/math/486938d1819df5972cb80ef2d101517b20412366.png" alt="\boldsymbol{a}" style="vertical-align: 0px"/>.
The parameters <img class="math" src="_images/math/486938d1819df5972cb80ef2d101517b20412366.png" alt="\boldsymbol{a}" style="vertical-align: 0px"/> will be fitted to achieve agreement
of the model and data.</p>
<p>In experiments we often have the situation that the measured <em>x</em> values
are subject to systematic errors caused, for example, by instrumental
zero shift or, in powder diffraction measurements,
by displacement of sample in the instrument.
If this is the case, such errors should be a part of the model.
In Fityk, this part of the model is called <em class="dfn">x-correction</em>.
The final formula for the model is:</p>
<div class="math" id="model-formula">
<p><img src="_images/math/d3d29774850d1fa81d750fef3ed9b65ee66c524e.png" alt="F(x; \boldsymbol{a}) = \sum_i f_i(x+Z(x; \boldsymbol{a}); \boldsymbol{a})" /></p>
</div><p>where <img class="math" src="_images/math/727873d2cbeda3d30f41bee18cb2bf5ea1071fa1.png" alt="Z(x; \boldsymbol{a}) = \sum_i z_i(x; \boldsymbol{a})" style="vertical-align: -5px"/>
is the <em>x</em>-correction. <em>Z</em> is constructed as a list of components,
analogously to <em>F</em>, although in practice it has rarely more than
one component.</p>
<p>Each component function is created by specifying a function type
and binding <em>variables</em> to type&#8217;s parameters. The next section explains
what are <em>variables</em> in Fityk, and then we get back to functions.</p>
<div class="section" id="variables">
<span id="id1"></span><h2>Variables</h2>
<p>Variables have names prefixed with the dollar symbol ($)
and are created by assigning a value:</p>
<div class="highlight-fityk"><div class="highlight"><pre>$foo=~5.3           <span class="c"># simple-variable</span>
$bar=5*sin($foo)    <span class="c"># compound-variable</span>
$c=3.1              <span class="c"># constant (the simplest compound-variable)</span>
</pre></div>
</div>
<p>The numbers prefixed with the tilde (~) are adjustable when the model
is fitted to the data.
Variable created by assigning <tt class="docutils literal"><span class="pre">~</span></tt><em>number</em>
(like <tt class="docutils literal"><span class="pre">$foo</span></tt> in the example above)
will be called a <em class="dfn">simple-variable</em>.</p>
<p>All other variables are called <em class="dfn">compound-variables</em>.
Compound variables either depend on other variables (<tt class="docutils literal"><span class="pre">$bar</span></tt> above)
or are constant (<tt class="docutils literal"><span class="pre">$c</span></tt>).</p>
<div class="admonition important">
<p class="first admonition-title">Important</p>
<p class="last">Unlike in popular programming languages, in Fityk variables store
formula, not a numeric value. If we define <tt class="docutils literal"><span class="pre">$b=2*$a</span></tt>,
every time <tt class="docutils literal"><span class="pre">$a</span></tt> changes, the value of <tt class="docutils literal"><span class="pre">$b</span></tt> will be recalculated.</p>
</div>
<p>To assign a value (constant) of another variable, use:
<tt class="docutils literal"><span class="pre">$b={$a}</span></tt>. Braces return the current value of the enclosed expression.
The left brace can be preceded by the tilde (<tt class="docutils literal"><span class="pre">~</span></tt>).
The assignment <tt class="docutils literal"><span class="pre">$b=~{$a}</span></tt> creates a simple variable.</p>
<p>Compound-variables can be build using operators +, -, *, /, ^
and the functions
<tt class="docutils literal"><span class="pre">sqrt</span></tt>,
<tt class="docutils literal"><span class="pre">exp</span></tt>,
<tt class="docutils literal"><span class="pre">log10</span></tt>,
<tt class="docutils literal"><span class="pre">ln</span></tt>,
<tt class="docutils literal"><span class="pre">sin</span></tt>,
<tt class="docutils literal"><span class="pre">cos</span></tt>,
<tt class="docutils literal"><span class="pre">tan</span></tt>,
<tt class="docutils literal"><span class="pre">sinh</span></tt>,
<tt class="docutils literal"><span class="pre">cosh</span></tt>,
<tt class="docutils literal"><span class="pre">tanh</span></tt>,
<tt class="docutils literal"><span class="pre">atan</span></tt>,
<tt class="docutils literal"><span class="pre">asin</span></tt>,
<tt class="docutils literal"><span class="pre">acos</span></tt>,
<tt class="docutils literal"><span class="pre">erf</span></tt>,
<tt class="docutils literal"><span class="pre">erfc</span></tt>,
<tt class="docutils literal"><span class="pre">lgamma</span></tt>,
<tt class="docutils literal"><span class="pre">abs</span></tt>,
<tt class="docutils literal"><span class="pre">voigt</span></tt>.
This is a subset of the functions used in
<a class="reference internal" href="data.html#transform"><em>data transformations</em></a>.</p>
<p>The braces may contain any data expression:</p>
<div class="highlight-fityk"><div class="highlight"><pre>$x0 = {x[0]}
$min_y = {min(y if a)}
$c = {max2($a, $b)}
</pre></div>
</div>
<p>Sometimes it is useful to freeze a variable, i.e. to prevent it from
changing while fitting:</p>
<div class="highlight-fityk"><div class="highlight"><pre>$a = ~12.3 <span class="c"># $a is fittable (simple-variable)</span>
$a = {$a}  <span class="c"># $a is not fittable (constant)</span>
$a = ~{$a} <span class="c"># $a is fittable (simple-variable) again</span>
</pre></div>
</div>
<div class="admonition-in-the-gui admonition ">
<p class="first admonition-title">In the GUI</p>
<p class="last">a variable can be switched between constant and simple-variable
by clicking the padlock button on the sidebar.
The icons <img alt="open lock" src="_images/open_lock_icon1.png" /> and <img alt="lock" src="_images/lock_icon1.png" />
show that the variable is fittable and frozen, respectively.</p>
</div>
<p>If the assigned expression contains tildes:</p>
<div class="highlight-fityk"><div class="highlight"><pre>$bleh=~9.1*exp(~2)
</pre></div>
</div>
<p>it automatically creates simple-variables corresponding
to the tilde-prefixed numbers.
In the example above two simple-variables (with values 9.1 and 2) are created.
Automatically created variables are named <tt class="docutils literal"><span class="pre">$_1</span></tt>, <tt class="docutils literal"><span class="pre">$_2</span></tt>, <tt class="docutils literal"><span class="pre">$_3</span></tt>, and so on.</p>
<p>Variables can be deleted using the command:</p>
<div class="highlight-fityk"><div class="highlight"><pre>delete $variable
</pre></div>
</div>
<p id="domain">Some fitting algorithms randomize the parameters of the model
(i.e. they randomize simple variables). To effectively use such algorithms,
the user should specify a <em class="dfn">domain</em> for each simple-variable,
i.e. the minimum and maximum value.
The domain does not imply any constraints on the value
the variable can have &#8211; it is only a hint for fitting algorithms.</p>
<p>The default algorithm (Lev-Mar) does not need it, so in most cases you
do not need to worry about domains.</p>
<p>Domains are used by the Nelder-Mead method and Genetic Algorithms.
The syntax is as follows:</p>
<div class="highlight-fityk"><div class="highlight"><pre>$a = ~12.3 [0:20] <span class="c"># initial values are drawn from the (0, 20) range</span>
</pre></div>
</div>
<p>If the domain is not specified, the default domain is used, which is
±<em>p</em>% of the current value, where <em>p</em> can be set using the
<em class="xref std std-option">domain_percent</em> option.</p>
</div>
<div class="section" id="function-types-and-functions">
<h2>Function Types and Functions</h2>
<p>Function types have names that start with upper case letter,
e.g. <tt class="docutils literal"><span class="pre">Linear</span></tt> or <tt class="docutils literal"><span class="pre">Voigt</span></tt>.</p>
<p>Functions have names prefixed with the percent symbol,
e.g. <tt class="docutils literal"><span class="pre">%func</span></tt>. Every function has a type and variables bound to its
parameters.</p>
<p>Functions can be created by giving the type and the correct
number of variables in brackets, e.g.:</p>
<div class="highlight-fityk"><div class="highlight"><pre>%f1 = Gaussian(~66254., ~24.7, ~0.264)
%f2 = Gaussian(~6e4, $ctr, $b+$c)
%f3 = Gaussian(height=~66254., hwhm=~0.264, center=~24.7)
</pre></div>
</div>
<p>Every expression which is valid on the right-hand side of a variable
assignment can be used as a variable.
If it is not just a name of a variable, an automatic variable is created.
In the above examples, two variables were implicitely created for <tt class="docutils literal"><span class="pre">%f2</span></tt>:
first for value <tt class="docutils literal"><span class="pre">6e4</span></tt> and the second for <tt class="docutils literal"><span class="pre">$b+$c</span></tt>).</p>
<p>If the names of function&#8217;s parameters are given (like for <tt class="docutils literal"><span class="pre">%f3</span></tt>),
the variables can be given in any order.</p>
<p>Function types can can have specified default values for
some parameters. The variables for such parameters can be omitted,
e.g.:</p>
<div class="highlight-fityk"><div class="highlight"><pre><span class="gp">=-&gt; </span>i Pearson7
Pearson7(height, center, hwhm, shape=2) = height/(1+((x-center)/hwhm)^2*(2^(1/shape)-1))^shape
<span class="gp">=-&gt; </span>%f4 = Pearson7(height=~66254., center=~24.7, fwhm=~0.264) <span class="c"># no shape is given</span>
New function %f4 was created.
</pre></div>
</div>
<p>Functions can be copied. The following command creates a deep copy
(i.e. all variables are also duplicated) of %foo:</p>
<div class="highlight-fityk"><div class="highlight"><pre>%bar = copy(%foo)
</pre></div>
</div>
<p>Functions can be also created with the command <tt class="docutils literal"><span class="pre">guess</span></tt>,
as described in <a class="reference internal" href="#guess"><em>Guessing Initial Parameters</em></a>.</p>
<p>Variables bound to the function parameters can be changed at any time:</p>
<div class="highlight-fityk"><div class="highlight"><pre><span class="gp">=-&gt; </span>%f = Pearson7(height=~66254., center=~24.7, fwhm=~0.264)
New function %f was created.
<span class="gp">=-&gt; </span>%f.center=~24.8
<span class="gp">=-&gt; </span>$h = ~66254
<span class="gp">=-&gt; </span>%f.height=$h
<span class="gp">=-&gt; </span>info %f
%f = Pearson7($h, $_5, $_3, $_4)
<span class="gp">=-&gt; </span>$h = ~60000 <span class="c"># variables are kept by name, so this also changes %f</span>
<span class="gp">=-&gt; </span>%p1.center = %p2.center + 3 <span class="c"># keep fixed distance between %p1 and %p2</span>
</pre></div>
</div>
<p>Functions can be deleted using the command:</p>
<div class="highlight-fityk"><div class="highlight"><pre>delete %function
</pre></div>
</div>
</div>
<div class="section" id="built-in-functions">
<span id="flist"></span><h2>Built-In Functions</h2>
<p>The list of all functions can be obtained using <tt class="docutils literal"><span class="pre">i</span> <span class="pre">types</span></tt>.
Some formulae here have long parameter names
(like &#8220;height&#8221;, &#8220;center&#8221; and &#8220;hwhm&#8221;) replaced with <img class="math" src="_images/math/412787c048e28774dc63fc27db42dc52ca858de7.png" alt="a_i" style="vertical-align: -3px"/></p>
<p><strong>Gaussian:</strong></p>
<div class="math">
<p><img src="_images/math/9e9c8a0fb6a92d4c3f969ab7a959a80f3069e3c9.png" alt="y = a_0
    \exp\left[-\ln(2)\left(\frac{x-a_1}{a_2}\right)^{2}\right]" /></p>
</div><p><strong>SplitGaussian:</strong></p>
<div class="math">
<p><img src="_images/math/441e7ddea6580ae19be0583f35b648340340f789.png" alt="y(x;a_0,a_1,a_2,a_3) = \begin{cases}
\textrm{Gaussian}(x;a_0,a_1,a_2) &amp; x\leq a_1\\
\textrm{Gaussian}(x;a_0,a_1,a_3) &amp; x&gt;a_1\end{cases}" /></p>
</div><p><strong>GaussianA:</strong></p>
<div class="math">
<p><img src="_images/math/19f15eacccc345c3179259320491956f3f23887a.png" alt="y = \sqrt{\frac{\ln(2)}{\pi}}\frac{a_0}{a_2}
    \exp\left[-\ln(2)\left(\frac{x-a_1}{a_2}\right)^{2}\right]" /></p>
</div><p><strong>Lorentzian:</strong></p>
<div class="math">
<p><img src="_images/math/bcdaf43e7130e8f85d7721440139a9481b23e631.png" alt="y = \frac{a_0}{1+\left(\frac{x-a_1}{a_2}\right)^2}" /></p>
</div><p><strong>SplitLorentzian:</strong></p>
<div class="math">
<p><img src="_images/math/3dd0773863fdeacd262d797c931a040801c71e90.png" alt="y(x;a_0,a_1,a_2,a_3) = \begin{cases}
\textrm{Lorentzian}(x;a_0,a_1,a_2) &amp; x\leq a_1\\
\textrm{Lorentzian}(x;a_0,a_1,a_3) &amp; x&gt;a_1\end{cases}" /></p>
</div><p><strong>LorentzianA:</strong></p>
<div class="math">
<p><img src="_images/math/707fc7dbf98d8f4f39c6bdf3f6c025ed948789c9.png" alt="y = \frac{a_0}{\pi a_2\left[1+\left(\frac{x-a_1}{a_2}\right)^2\right]}" /></p>
</div><p><strong>Pearson VII (Pearson7):</strong></p>
<div class="math">
<p><img src="_images/math/deaf305680f8dc36524842ef15f13f33789dbbd4.png" alt="y = \frac{a_0} {\left[1+\left(\frac{x-a_1}{a_2}\right)^2
                        \left(2^{\frac{1}{a_3}}-1\right)\right]^{a_3}}" /></p>
</div><p><strong>split Pearson VII (SplitPearson7):</strong></p>
<div class="math">
<p><img src="_images/math/695f416425dbcafe8c762a1e8c533023350a525d.png" alt="y(x;a_{0},a_{1},a_{2},a_{3},a_{4},a_{5}) = \begin{cases}
 \textrm{Pearson7}(x;a_0,a_1,a_2,a_4) &amp; x\leq a_1\\
 \textrm{Pearson7}(x;a_0,a_1,a_3,a_5) &amp; x&gt;a_1\end{cases}" /></p>
</div><p><strong>Pearson VII Area (Pearson7A):</strong></p>
<div class="math">
<p><img src="_images/math/9f4a323840470c45eb00c5896ba579edc0f0799f.png" alt="y = \frac{a_0\Gamma(a_3)\sqrt{2^{\frac{1}{a_3}}-1}}
         {a_2\Gamma(a_3-\frac{1}{2})\sqrt{\pi} \left[
            1 + \left(\frac{x-a_1}{a_2}\right)^2
                \left(2^{\frac{1}{a_3}}-1\right)
         \right]^{a_3}}" /></p>
</div><p><strong>Pseudo-Voigt (PseudoVoigt):</strong></p>
<div class="math">
<p><img src="_images/math/c2ec895a1a42f04814ac1e370c4b8ca8fb702167.png" alt="y = a_0 \left[(1-a_3)\exp\left(-\ln(2)\left(\frac{x-a_1}{a_2}\right)^2\right)
              + \frac{a_3}{1+\left(\frac{x-a_1}{a_2}\right)^2}
        \right]" /></p>
</div><p>Pseudo-Voigt is a name given to the sum of Gaussian and Lorentzian.
<img class="math" src="_images/math/572b65ebc3f438b176b4ca4a890799f2f72564af.png" alt="a_3" style="vertical-align: -3px"/> parameters in Pearson VII and Pseudo-Voigt
are not related.</p>
<p><strong>split Pseudo-Voigt (SplitPseudoVoigt):</strong></p>
<div class="math">
<p><img src="_images/math/e847b12d44bdb9991047fa3001a2d1f23a077769.png" alt="y(x;a_{0},a_{1},a_{2},a_{3},a_{4},a_{5}) = \begin{cases}
 \textrm{PseudoVoigt}(x;a_0,a_1,a_2,a_4) &amp; x\leq a_1\\
 \textrm{PseudoVoigt}(x;a_0,a_1,a_3,a_5) &amp; x&gt;a_1\end{cases}" /></p>
</div><p><strong>Pseudo-Voigt Area (PseudoVoigtA):</strong></p>
<div class="math">
<p><img src="_images/math/7508455f7c089e149392451dec57d771370d63ce.png" alt="y = a_0 \left[\frac{(1-a_3)\sqrt{\ln(2)}}{a_2\sqrt{\pi}}
              \exp\left(-\ln2\left(\frac{x-a_1}{a_2}\right)^2\right)
              + \frac{a_3}{\pi a_2
                           \left[1+\left(\frac{x-a_1}{a_2}\right)^2\right]}
        \right]" /></p>
</div><p><strong>Voigt:</strong></p>
<div class="math">
<p><img src="_images/math/98327fe212a6e72966017a3e21780935b66080f8.png" alt="y = \frac
    {a_0 \int_{-\infty}^{+\infty}
             \frac{\exp(-t^2)}{a_3^2+(\frac{x-a_1}{a_2}-t)^2} dt}
    {\int_{-\infty}^{+\infty}
             \frac{\exp(-t^2)}{a_3^2+t^2} dt}" /></p>
</div><p>The Voigt function is a convolution of Gaussian and Lorentzian functions.
<img class="math" src="_images/math/ee5c7c921b3334f6ecf58c241eeea42acf0ce71e.png" alt="a_0" style="vertical-align: -3px"/> = heigth,
<img class="math" src="_images/math/1ab39d761413804680d26d972381f028001562f5.png" alt="a_1" style="vertical-align: -4px"/> = center,
<img class="math" src="_images/math/5e97a8af68fbc8e357d3ee0eba452022b06c1875.png" alt="a_2" style="vertical-align: -3px"/> is proportional to the Gaussian width, and
<img class="math" src="_images/math/572b65ebc3f438b176b4ca4a890799f2f72564af.png" alt="a_3" style="vertical-align: -3px"/> is proportional to the ratio of Lorentzian and Gaussian widths.</p>
<p>Voigt is computed according to R.J.Wells,
<em>Rapid approximation to the Voigt/Faddeeva function and its derivatives</em>,
Journal of Quantitative Spectroscopy &amp; Radiative Transfer
62 (1999) 29-48.
(See also: <a class="reference external" href="http://www.atm.ox.ac.uk/user/wells/voigt.html">http://www.atm.ox.ac.uk/user/wells/voigt.html</a>).
The approximation is very fast, but not very exact.</p>
<p>FWHM is estimated using the approximation by Olivero and Longbothum
(<a class="reference external" href="http://dx.doi.org/10.1016/0022-4073(77)90161-3">JQSRT 17, 233 (1977)</a>):
<img class="math" src="_images/math/38b394ef8f2ecd34817add552d9daa3fe8b9e017.png" alt="0.5346 w_L + \sqrt{0.2169 w_L^2 + w_G^2}" style="vertical-align: -6px"/>.</p>
<p><strong>VoigtA:</strong></p>
<div class="math">
<p><img src="_images/math/23decadd8b245289f5fa993f2c99e0ac9cff56dc.png" alt="y = \frac{a_0}{\sqrt{\pi}a_2}
    \int_{-\infty}^{+\infty}
        \frac{\exp(-t^2)}{a_3^2+(\frac{x-a_1}{a_2}-t)^2} dt" /></p>
</div><p><strong>Exponentially Modified Gaussian (EMG):</strong></p>
<div class="math">
<p><img src="_images/math/9fabdcba50c720ea1ae42cd0b4f1d3eb5f367e56.png" alt="y = \frac{ac\sqrt{2\pi}}{2d}
    \exp\left(\frac{b-x}{d}+\frac{c^2}{2d^2}\right)
    \left[\frac{d}{\left|d\right|}
          -\textrm{erf}\left(\frac{b-x}{\sqrt{2}c}
                             + \frac{c}{\sqrt{2}d}\right)
    \right]" /></p>
</div><p><strong>LogNormal:</strong></p>
<div class="math">
<p><img src="_images/math/b72439b28d639f1999aeecd537d21df605ad8db8.png" alt="y = h \exp\left\{ -\ln(2) \left[
                                \frac{\ln\left(1+2b\frac{x-c}{w}\right)}{b}
                         \right]^{2} \right\}" /></p>
</div><p><strong>Doniach-Sunjic (DoniachSunjic):</strong></p>
<div class="math">
<p><img src="_images/math/7567e40901ed423f42f5fc972bca0fd2d311ec40.png" alt="y = \frac{h\left[\frac{\pi a}{2}
                 + (1-a)\arctan\left(\frac{x-E}{F}\right)\right]}
         {F+(x-E)^2}" /></p>
</div><p><strong>Polynomial5:</strong></p>
<div class="math">
<p><img src="_images/math/349202f044a9aca8885d8084005ea099f1816b94.png" alt="y = a_0 + a_1 x +a_2 x^2 + a_3 x^3 + a_4 x^4 + a_5 x^5" /></p>
</div></div>
<div class="section" id="variadic-functions">
<h2>Variadic Functions</h2>
<p><em>Variadic</em> function types have variable number of parameters.
Two variadic function types are defined:</p>
<div class="highlight-fityk"><div class="highlight"><pre>Spline(x1, y1, x2, y2, ...)
Polyline(x1, y1, x2, y2, ...)
</pre></div>
</div>
<p>This example:</p>
<div class="highlight-fityk"><div class="highlight"><pre>%f = Spline(22.1, 37.9, 48.1, 17.2, 93.0, 20.7)
</pre></div>
</div>
<p>creates a function that is a <em>natural cubic spline</em> interpolation
through points (22.1, 37.9), (48.1, 17.2), ....</p>
<p>The <tt class="docutils literal"><span class="pre">Polyline</span></tt> function is a polyline interpolation (spline of order 1).</p>
<p>Both <tt class="docutils literal"><span class="pre">Spline</span></tt> and <tt class="docutils literal"><span class="pre">Polyline</span></tt> functions are primarily used
for the manual baseline subtraction via the GUI.</p>
<p>The derivatives of Spline function are not calculated, so this function
is not refined by the default, derivative-based fitting algorithm.</p>
<p>Since the Polyline derivatives are calculated, it is possible to perform
weighted least squares approximation by broken lines, although
non-linear fitting algorithms are not optimal for this task.</p>
</div>
<div class="section" id="user-defined-functions-udf">
<span id="udf"></span><h2>User-Defined Functions (UDF)</h2>
<p>User-defined function types can be added using command <tt class="docutils literal"><span class="pre">define</span></tt>,
and then used in the same way as built-in functions.</p>
<p>Example:</p>
<div class="highlight-fityk"><div class="highlight"><pre>define MyGaussian(height, center, hwhm) = height*exp(-ln(2)*((x-center)/hwhm)^2)
</pre></div>
</div>
<ul>
<li><p class="first">The name of new type must start with an upper-case letter,
contain only letters and digits and have at least two characters.</p>
</li>
<li><p class="first">The name of the type is followed by parameters in brackets.</p>
</li>
<li><p class="first">Parameter name must start with lowercase letter and,
contain only lowercase letters, digits and the underscore (&#8216;_&#8217;).</p>
</li>
<li><p class="first">The name &#8220;x&#8221; is reserved, do not put it into parameter list,
just use it on the right-hand side of the definition.</p>
</li>
<li><p class="first">There are special names of parameters that Fityk understands:</p>
<ul class="simple">
<li>if the functions is peak-like:
<tt class="docutils literal"><span class="pre">height</span></tt>, <tt class="docutils literal"><span class="pre">center</span></tt>, <tt class="docutils literal"><span class="pre">hwhm</span></tt>, <tt class="docutils literal"><span class="pre">area</span></tt>,</li>
<li>if the function is more like linear:
<tt class="docutils literal"><span class="pre">slope</span></tt>, <tt class="docutils literal"><span class="pre">intercept</span></tt>, <tt class="docutils literal"><span class="pre">avgy</span></tt>.</li>
</ul>
<p>The initial values of these parameters can be guessed (command <tt class="docutils literal"><span class="pre">guess</span></tt>)
from the data.  <tt class="docutils literal"><span class="pre">hwhm</span></tt> means half width at half maximum,
the other names are self-explaining.</p>
</li>
<li><p class="first">Each parameter may have a default value (see the examples below).
The default value can be either a number or an expression that depends
on the parameters listed above (e.g. <tt class="docutils literal"><span class="pre">0.8*hwhm</span></tt>).
The default value always binds a simple-variable to the parameter.</p>
</li>
</ul>
<p>UDFs can be defined in a few ways:</p>
<ul class="simple">
<li>by giving a full formula, like in the example above,</li>
<li>as a <em class="dfn">re-parametrization</em> of existing function
(see the <tt class="docutils literal"><span class="pre">GaussianArea</span></tt> example below),</li>
<li>as a sum of already defined functions
(see the <tt class="docutils literal"><span class="pre">GLSum</span></tt> example below),</li>
<li>as a splitted (bifurcated) function:
<tt class="docutils literal"><span class="pre">x</span> <span class="pre">&lt;</span></tt> <em>expression</em> <tt class="docutils literal"><span class="pre">?</span></tt> <em>Function1(...)</em> <tt class="docutils literal"><span class="pre">:</span></tt> <em>Function2(...)</em>
(see the <tt class="docutils literal"><span class="pre">SplitL</span></tt> example below).</li>
</ul>
<p>When giving a full formula, the right-hand side of the equality sign
is similar to the <a class="reference internal" href="#variables"><em>definiton of variable</em></a>,
but the formula can also depend on <em>x</em>.
Hopefully the examples can make the syntax clear:</p>
<div class="highlight-fityk"><div class="highlight"><pre><span class="c"># this is how some built-in functions could be defined</span>
define MyGaussian(height, center, hwhm) = height*exp(-ln(2)*((x-center)/hwhm)^2)
define MyLorentzian(height, center, hwhm) = height/(1+((x-center)/hwhm)^2)
define MyCubic(a0=height,a1=0, a2=0, a3=0) = a0 + a1*x + a2*x^2 + a3*x^3

<span class="c"># supersonic beam arrival time distribution</span>
define SuBeArTiDi(c, s, v0, dv) = c*(s/x)^3*exp(-(((s/x)-v0)/dv)^2)/x

<span class="c"># area-based Gaussian can be defined as modification of built-in Gaussian</span>
<span class="c"># (it is the same as built-in GaussianA function)</span>
define GaussianArea(area, center, hwhm) = Gaussian(area/hwhm/sqrt(pi/ln(2)), center, hwhm)

<span class="c"># sum of Gaussian and Lorentzian, a.k.a. PseudoVoigt (should be in one line)</span>
define GLSum(height, center, hwhm, shape) = Gaussian(height*(1-shape), center, hwhm)
+ Lorentzian(height*shape, center, hwhm)

<span class="c"># split-Gaussian, the same as built-in SplitGaussian (should be in one line)</span>
define SplitG(height, center, hwhm1=fwhm*0.5, hwhm2=fwhm*0.5) =
  x &lt; center ? Lorentzian(height, center, hwhm1)
             : Lorentzian(height, center, hwhm2)
</pre></div>
</div>
<p>There is a simple substitution mechanism that makes writing complicated
functions easier.
Substitutions must be assigned in the same line, after the keyword <tt class="docutils literal"><span class="pre">where</span></tt>.</p>
<p>Example:</p>
<div class="highlight-fityk"><div class="highlight"><pre>define ReadShockley(sigma0=1, a=1) = sigma0 * t * (a - ln(t)) where t=x*pi/180

<span class="c"># more complicated example, with nested substitutions</span>
define FullGBE(k, alpha) = k * alpha * eta * (eta / tanh(eta) - ln (2*sinh(eta))) where eta = 2*pi/alpha * sin(theta/2), theta=x*pi/180
</pre></div>
</div>
<div class="admonition-how-it-works-internally admonition ">
<p class="first admonition-title">How it works internally</p>
<p>The formula is parsed,
derivatives of the formula are calculated symbolically,
expressions are simplified
and bytecode for virtual machine (VM) is created.</p>
<p class="last">When fitting, the VM calculates the value of the function
and derivatives for every point.</p>
</div>
<p>Defined functions can be undefined using command <tt class="docutils literal"><span class="pre">undefine</span></tt>:</p>
<div class="highlight-fityk"><div class="highlight"><pre>undefine GaussianArea
</pre></div>
</div>
<p>It is common to add own definitions to the <tt class="file docutils literal"><span class="pre">init</span></tt> file.
See the section <a class="reference internal" href="ref.html#invoking"><em>Starting fityk and cfityk</em></a> for details.</p>
</div>
<div class="section" id="cutoff">
<span id="function-cutoff"></span><h2>Cutoff</h2>
<p>With default settings, the value of every function is calculated
at every point. Peak functions, such as Gaussian, often have non-negligible
values only in a small fraction of all points,
so if you have many narrow peaks
(like <a class="reference external" href="http://commons.wikimedia.org/wiki/File:Diff_NaBr.png">here</a>),
the basic optimization is to calculate values of each peak function
only near the function&#8217;s center.
If the option <em class="xref std std-option">function_cutoff</em> is set to a non-zero value,
each function is evaluated only in the range where its values are
greater than the <em class="xref std std-option">function_cutoff</em>.</p>
<p>This optimization is supported only by some built-in functions.</p>
</div>
<div class="section" id="model-f-and-z">
<h2>Model, F and Z</h2>
<p>As already discussed, each dataset has a separate model
that can be fitted to the data.
As can be seen from the <a class="reference internal" href="#model-formula"><em>formula</em></a> at the beginning
of this chapter, the model is defined as a set functions <img class="math" src="_images/math/61c594c97b61f72af93af4e61894b3f16a3bbb30.png" alt="f_i" style="vertical-align: -4px"/>
and a set of functions <img class="math" src="_images/math/7532b41a34c9ca592694135d1de65eeb8d9ce036.png" alt="z_i" style="vertical-align: -3px"/>.
These sets are named <em>F</em> and <em>Z</em> respectively.
The model is constructed by specifying names of functions in these two sets.</p>
<p>In many cases <em class="dfn">x-correction</em> Z is not used.
The fitted curve is thus the sum of all functions in F.</p>
<p>Command:</p>
<div class="highlight-fityk"><div class="highlight"><pre>F += %function
</pre></div>
</div>
<p>adds  <em>%function</em> to F, and</p>
<div class="highlight-fityk"><div class="highlight"><pre>Z += %function
</pre></div>
</div>
<p>adds <em>%function</em> to Z.</p>
<p>A few examples:</p>
<div class="highlight-fityk"><div class="highlight"><pre><span class="c"># create and add function to F</span>
%g = Gaussian(height=~66254., hwhm=~0.264, center=~24.7)
F += %g

<span class="c"># create unnamed function and add it to F</span>
F += Gaussian(height=~66254., hwhm=~0.264, center=~24.7)

<span class="c"># clear F</span>
F = 0

<span class="c"># clear F and put three functions in it</span>
F = %a + %b + %c

<span class="c"># show info about the first and the last function in F</span>
info F[0], F[-1]
</pre></div>
</div>
<p>The next sections shows an easier way to add a function (command <tt class="docutils literal"><span class="pre">guess</span></tt>).</p>
<p>If there is more than one dataset, F and Z can be prefixed
with the dataset number (e.g. <tt class="docutils literal"><span class="pre">&#64;1.F</span></tt>).</p>
<p>The model can be copied. To copy the model from <tt class="docutils literal"><span class="pre">&#64;0</span></tt> to <tt class="docutils literal"><span class="pre">&#64;1</span></tt>
we type one of the two commands:</p>
<div class="highlight-fityk"><div class="highlight"><pre>@1.F = @0.F        <span class="c"># shallow copy</span>
@1.F = copy(@0.F)  <span class="c"># deep copy</span>
</pre></div>
</div>
<p>The former command uses the same functions in both models: if you shift
a peak in <tt class="docutils literal"><span class="pre">&#64;1</span></tt>, it will be also shifted in <tt class="docutils literal"><span class="pre">&#64;0</span></tt>. The latter command
(deep copy) duplicates all functions and variables and makes an independent
model.</p>
<div class="admonition-in-the-gui admonition ">
<p class="first admonition-title">In the GUI</p>
<p class="last">click the button <img alt="Copy-Model" class="icon" src="_images/copyfunc_icon1.png" /> on the sidebar to make a deep copy.</p>
</div>
<p>It is often required to keep the width or shape of peaks constant
for all peaks in the dataset. To change the variables bound to parameters
with a given name for all functions in F, use the command:</p>
<div class="highlight-fityk"><div class="highlight"><pre>F[*].param = variable
</pre></div>
</div>
<p>Examples:</p>
<div class="highlight-fityk"><div class="highlight"><pre><span class="c"># Set hwhm of all functions in F that have a parameter hwhm to $foo</span>
<span class="c"># (hwhm here means half-width-at-half-maximum)</span>
F[*].hwhm = $foo

<span class="c"># Bound the variable used for the shape of peak %_1 to shapes of all</span>
<span class="c"># functions in F</span>
F[*].shape = %_1.shape

<span class="c"># Create a new simple-variable for each function in F and bound the</span>
<span class="c"># variable to parameter hwhm. All hwhm parameters will be independent.</span>
F[*].hwhm = ~0.2
</pre></div>
</div>
<div class="admonition-in-the-gui admonition ">
<p class="first admonition-title">In the GUI</p>
<p class="last">the buttons <tt class="docutils literal"><span class="pre">=W</span></tt> and <tt class="docutils literal"><span class="pre">=S</span></tt> on the sidebar make, respectively,
the HWHM and shape of all functions the same. Pressing the buttons
again will make all the parameters independent.</p>
</div>
</div>
<div class="section" id="guessing-initial-parameters">
<span id="guess"></span><h2>Guessing Initial Parameters</h2>
<p>The program can automatically set initial parameters of peaks (using
peak-detection algorithm) and lines (using linear regression).
Choosing initial parameters of a function by the program
will be called <em class="dfn">guessing</em>.</p>
<p>It is possible to guess peak location and add it to <em>F</em> with the command:</p>
<div class="highlight-fityk"><div class="highlight"><pre>guess [%name =] PeakType [(initial values...)] [[x1:x2]]
</pre></div>
</div>
<p>Examples:</p>
<div class="highlight-fityk"><div class="highlight"><pre><span class="c"># add Gaussian in the given range</span>
@0: guess Gaussian [22.1:30.5]

<span class="c"># the same, but name the new function %f1</span>
@0: guess %f1 = Gaussian [22.1:30.5]

<span class="c"># search for the peak in the whole dataset</span>
@0: guess Gaussian

<span class="c"># add one Gaussian to each dataset</span>
@*: guess Gaussian

<span class="c"># set the center and shape explicitely (determine height and width)</span>
guess PseudoVoigt(center=$ctr, shape=~0.3) [22.1:30.5]
</pre></div>
</div>
<ul class="simple">
<li>Name of the function is optional.</li>
<li>Some of the parameters can be specified in brackets.</li>
<li>If the range is omitted, the whole dataset will be searched.</li>
</ul>
<p>Fityk offers a simple algorithm for peak-detection.
It finds the highest point in the given range (<tt class="docutils literal"><span class="pre">center</span></tt> and <tt class="docutils literal"><span class="pre">height</span></tt>),
and than tries to find the width of the peak (<tt class="docutils literal"><span class="pre">hwhm</span></tt>, and <tt class="docutils literal"><span class="pre">area</span></tt>
= <em>height</em> × <em>hwhm</em>).</p>
<p>If the highest point is at boundary of the given range,
the points from the boundary to the nearest local minimum are ignored.</p>
<p>The values of height and width found by the algorithm
are multiplied by the values of options <em class="xref std std-option">height_correction</em>
and <em class="xref std std-option">width_correction</em>, respectively. The default value for both
options is 1.</p>
<p>The linear traits <tt class="docutils literal"><span class="pre">slope</span></tt> and <tt class="docutils literal"><span class="pre">intercept</span></tt> are calculated using linear
regression (without weights of points).
<tt class="docutils literal"><span class="pre">avgy</span></tt> is calculated as average value of <em>y</em>.</p>
<div class="admonition-in-the-gui admonition ">
<p class="first admonition-title">In the GUI</p>
<p>select a function from the list of functions on the toolbar
and press <img alt="Auto Add" class="icon" src="_images/add_peak_icon.png" /> to add (guess) the selected function.</p>
<p class="last">To choose a data range change the GUI mode to <img alt="Add-Peak Mode" class="icon" src="_images/mode_add_icon.png" />
and select the range with the right mouse button.</p>
</div>
</div>
<div class="section" id="displaying-information">
<h2>Displaying Information</h2>
<p>The <tt class="docutils literal"><span class="pre">info</span></tt> command can be show useful information when constructing
the model.</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">info</span> <span class="pre">types</span></tt></dt>
<dd>shows the list of available function types.</dd>
<dt><tt class="docutils literal"><span class="pre">info</span> <span class="pre">FunctionType</span></tt></dt>
<dd>(e.g. <tt class="docutils literal"><span class="pre">info</span> <span class="pre">Pearson7</span></tt>) shows the formula (definition).</dd>
<dt><tt class="docutils literal"><span class="pre">info</span> <span class="pre">guess</span> <span class="pre">[range]</span></tt></dt>
<dd>shows where the <tt class="docutils literal"><span class="pre">guess</span></tt> command would locate a peak.</dd>
<dt><tt class="docutils literal"><span class="pre">info</span> <span class="pre">functions</span></tt></dt>
<dd>lists all defined functions.</dd>
<dt><tt class="docutils literal"><span class="pre">info</span> <span class="pre">variables</span></tt></dt>
<dd>lists all defined variables.</dd>
<dt><tt class="docutils literal"><span class="pre">info</span> <span class="pre">F</span></tt></dt>
<dd>lists components of <em>F</em>.</dd>
<dt><tt class="docutils literal"><span class="pre">info</span> <span class="pre">Z</span></tt></dt>
<dd>lists components of <em>Z</em>.</dd>
<dt><tt class="docutils literal"><span class="pre">info</span> <span class="pre">formula</span></tt></dt>
<dd>shows the full mathematical formula of the fitted model.</dd>
<dt><tt class="docutils literal"><span class="pre">info</span> <span class="pre">simplified_formula</span></tt></dt>
<dd>shows the same, but the formula is simplified.</dd>
<dt><tt class="docutils literal"><span class="pre">info</span> <span class="pre">gnuplot_formula</span></tt></dt>
<dd>shows same as <tt class="docutils literal"><span class="pre">formula</span></tt>, but the output is readable by gnuplot,
e.g. <tt class="docutils literal"><span class="pre">x^2</span></tt> is replaced by  <tt class="docutils literal"><span class="pre">x**2</span></tt>.</dd>
<dt><tt class="docutils literal"><span class="pre">info</span> <span class="pre">simplified_gnuplot_formula</span></tt></dt>
<dd>shows the simplified formula in the gnuplot format.</dd>
<dt><tt class="docutils literal"><span class="pre">info</span> <span class="pre">peaks</span></tt></dt>
<dd>show a formatted list of parameters of functions in <em>F</em>.</dd>
<dt><tt class="docutils literal"><span class="pre">info</span> <span class="pre">peaks_err</span></tt></dt>
<dd>shows the same data, additionally including uncertainties of the parameters.</dd>
<dt><tt class="docutils literal"><span class="pre">info</span> <span class="pre">models</span></tt></dt>
<dd>a script that reconstructs all variables, functions and models.</dd>
</dl>
<p>The last two commands are often redirected to a file
(<tt class="docutils literal"><span class="pre">info</span> <span class="pre">peaks</span> <span class="pre">&gt;</span> <span class="pre">filename</span></tt>).</p>
<p>The complete list of <tt class="docutils literal"><span class="pre">info</span></tt> arguments can be found in <a class="reference internal" href="ref.html#info"><em>Information Display</em></a>.</p>
<div class="admonition-in-the-gui admonition ">
<p class="first admonition-title">In the GUI</p>
<p class="last">most of the above commands has clickable equivalents.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="fit.html" title="Curve Fitting"
             accesskey="N">next</a></li>
        <li class="right" >
          <a href="data.html" title="Data"
             accesskey="P">previous</a> &nbsp; </li>
  <li><a href="http://fityk.nieto.pl/">Home</a>&nbsp;&nbsp;&nbsp;</li>
  <li><a href="https://github.com/wojdyr/fityk/wiki">Wiki</a>&nbsp;&nbsp;&nbsp;</li>
  
        <li><a href="fityk-manual.html">Manual</a> </li>
 
      </ul>
    </div>
    <div class="footer">
      Last updated on 2011-09-28.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>
  </body>
</html>